package oop.ex3.serverprotocol;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.LinkedList;


import oop.ex3.nameserver.NameServer;
import oop.ex3.sharedclasses.IPaddress;

/**
 * Handles WANTSERVERS Message.
 * handles a client's request to get all the nameServers known to 
 * the NameServer.
 * @author zablo
 *
 */
public class WantServers extends ClientMessageHandler{

	
	public boolean run(NameServer server, IPaddress sessionIP,
			DataInputStream clientInput, DataOutputStream clientOutput) {
		if (endsWithEnd(clientInput)==FAILURE){
			// the message doesn't end with END
			return FAILURE;
		}
		
		LinkedList<IPaddress> allKnownNameServers=server.getAllKnownNameServers();
		
		for (IPaddress nameServerAddress: allKnownNameServers){
			if (ServerMessageSender.containNameServer
					(clientOutput, nameServerAddress)==FAILURE){
				return FAILURE;
			}
			
			// TODO check - should I expect DONE END from the client when sending
			// it each nameServer?
		}
		
		if (ServerMessageSender.endList(clientOutput)==FAILURE){
			return FAILURE;
		}
		
		return SUCCESS;
	}
	

}
